package com.edu.web.controller.info;

import com.edu.common.core.domain.entity.SysUser;
import com.edu.system.domain.TTeacherExt;
import com.edu.system.service.ITTeacherExtService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.enums.BusinessType;
import com.edu.system.domain.TTeacher;
import com.edu.system.service.ITTeacherService;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师Controller
 *
 * @author xxxx
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/system/teacher")
public class TTeacherController extends BaseController {
    @Autowired
    private ITTeacherService tTeacherService;
    @Resource
    private ITTeacherExtService itTeacherExtService;

    /**
     * 查询教师列表
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTeacher tTeacher) {
        startPage();
        List<TTeacher> list = tTeacherService.selectTTeacherList(tTeacher);
        return getDataTable(list);
    }

    /**
     * 导出教师列表
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:export')")
    @Log(title = "教师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTeacher tTeacher) {
        List<TTeacher> list = tTeacherService.selectTTeacherList(tTeacher);
        ExcelUtil<TTeacher> util = new ExcelUtil<TTeacher>(TTeacher.class);
        util.exportExcel(response, list, "教师数据");
    }

    @Log(title = "批量导入教师", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:teacher:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TTeacher> util = new ExcelUtil<TTeacher>(TTeacher.class);
        List<TTeacher> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tTeacherService.importUser(userList, operName);
        return success(message);
    }

    /**
     * 获取教师详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tTeacherService.selectTTeacherById(id));
    }

    /**
     * 新增教师
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:add')")
    @Log(title = "教师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TTeacher tTeacher) {
        return toAjax(tTeacherService.insertTTeacher(tTeacher));
    }

    /**
     * 修改教师
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:edit')")
    @Log(title = "教师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTeacher tTeacher) {
        return toAjax(tTeacherService.updateTTeacher(tTeacher));
    }

    /**
     * 删除教师
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:remove')")
    @Log(title = "教师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTeacherService.deleteTTeacherByIds(ids));
    }

    /**
     * 获取教师list
     */
    @GetMapping(value = "/ext/allList")
    public AjaxResult allList() {
        return success(itTeacherExtService.selectTTeacherExtList(new TTeacherExt()));
    }

    @GetMapping(value = "/cnt")
    public AjaxResult getCnt() {
        return success(tTeacherService.getCnt());
    }
}
